package com.yy.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;


import com.yy.config.AlipayProperties;
import com.yy.service.IPayTradeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述
 * 作者 yy
 */
@RestController
public class CallbackController {

    @Autowired
    private AlipayProperties properties;

    @Autowired
    private IPayTradeOrderService payTradeOrderService;

    /**
     * 回调
     * @param outTradeNo 自己系统的订单号
     * @param request
     */
    @RequestMapping("callback/{outTradeNo}")
    public void callback(@PathVariable String outTradeNo, HttpServletRequest request){
        Map<String, String> dataMap = this.getParameterMap(request);
        try {
            boolean singVerified = AlipaySignature.rsaCheckV1(dataMap, properties.getAlipayPublicKey(), properties.getCharset(), properties.getSignType());
            System.out.println(singVerified);//只有支付宝调用我们系统的接口才能去更新系统订单状态
            System.out.println("验证签名结果:"+singVerified); //如果为True说明当前请求为支付宝调用
            if(singVerified){
                String trade_status = dataMap.get("trade_status");
                if(trade_status.equals("TRADE_SUCCESS")){
                    String refund_fee = dataMap.get("refund_fee"); //如果是支付的回调。回调的dataMap里面就没有refund_fee这个key 如果有就是明是退费的回调
                    if(refund_fee==null){
                        //支付成功
                        String tradeNo = dataMap.get("trade_no");//支付宝那边的订单号
                        System.out.println("支付成功。根据"+outTradeNo+"修改自系统里面的订单状态");
                        payTradeOrderService.paySuccess(outTradeNo,tradeNo);
                    }else{
                        //退费
                        System.out.println("退费成功");
                    }
                }else if(trade_status.equals("TRADE_CLOSED")){
                    System.out.println("交易关闭");
                }else if(trade_status.equals("TRADE_FINISHED")){
                    System.out.println("交易完结");
                }else if(trade_status.equals("WAIT_BUYER_PAY")){
                    System.out.println("交易创建,等待用户确定支付");
                }
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            System.out.println("验证签名出现异常");
        }
    }

    /**
     * 获取request中的参数集合转Map
     * Map<String,String> parameterMap = RequestUtil.getParameterMap(request)
     * @param request
     * @return
     */
    public Map<String, String> getParameterMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        return map;
    }
}
